Toriality's Blog

java_expressions_statements_blocks

created_at:

June 4, 2024 at 5:40 PM

last_updated:

July 15, 2024 at 8:11 PM

Expressions, Statements, and Blocks

Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks.

Expressions

An expression is a construct made up of variables, operators and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.

x + 10;

Statements

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

  • Assignment expressions;
  • Any use of ++ and --;
  • Method invocations;
  • Object creation expressions;
// assignment statement
aValue = 8933.234;
// increment statement
aValue++;
// method invocation
System.out.println("Hello, World!");
// object creation
Bicycle bicycle = new Bicycle();

There are two other kinds of statements: declaration and control flow statements. A declaration statement declares a variable.

double aValue = 8933.234;

Control flow statements regulate the order in which statements get executed. We will learn about it later.

Blocks

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.